<?php

namespace common\models\common;

use common\models\member\Member;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "rf_common_pay_log".
 * @property int    $id             主键
 * @property int    $merchant_id    商户id
 * @property string $order_sn       关联订单号
 * @property int    $order_group    组别[默认统一支付类型]
 * @property string $openid         openid
 * @property string $mch_id         商户支付账户
 * @property string $out_trade_no   商户订单号
 * @property string $transaction_id 微信订单号
 * @property double $total_fee      微信充值金额
 * @property string $fee_type       标价币种
 * @property int    $pay_type       支付类型[1:微信;2:支付宝;3:银联]
 * @property double $pay_fee        支付金额
 * @property int    $pay_status     支付状态
 * @property int    $pay_time       创建时间
 * @property string $trade_type     交易类型，取值为：JSAPI，NATIVE，APP等
 * @property string $refund_sn      退款编号
 * @property double $refund_fee     退款金额
 * @property int    $is_refund      退款情况[0:未退款;1已退款]
 * @property int    $status         状态[-1:删除;0:禁用;1启用]
 * @property int    $creat  ed_at     创建时间
 * @property int    $updated_at     修改时间
 */
class Capital extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {

        return '{{%common_pay_log}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [
                [
                    'merchant_id',
                    'pay_status',
                    'pay_time',
                    'is_refund',
                    'status',
                    'created_at',
                    'updated_at',
                    'openid',
                ],
                'integer',
            ],
            [['total_fee', 'pay_fee', 'refund_fee'], 'number'],
            [['order_sn', 'mch_id', 'order_group'], 'string', 'max' => 20],
            [['transaction_id'], 'string', 'max' => 50],
            [['out_trade_no'], 'string', 'max' => 32],
            [['fee_type'], 'string', 'max' => 10],
            [['trade_type'], 'string', 'max' => 16],
            [['refund_sn'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'             => '主键',
            'merchant_id'    => '商户id',
            'order_sn'       => '关联订单号',
            'order_group'    => '组别',
            'openid'         => '用户',
            'mch_id'         => '商户支付账户',
            'out_trade_no'   => '商户订单号',
            'transaction_id' => '微信订单号',
            'total_fee'      => '充值金额',
            'fee_type'       => '标价币种',
            'pay_type'       => '支付类型',
            'pay_fee'        => '支付金额',
            'pay_status'     => '支付状态',
            'pay_time'       => '创建时间',
            'trade_type'     => '交易类型',
            'refund_sn'      => '退款编号',
            'refund_fee'     => '退款金额',
            'is_refund'      => '退款情况',
            'status'         => '状态',
            'created_at'     => '创建时间',
            'updated_at'     => '修改时间',
        ];
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class'      => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at', 'pay_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ];
    }

    /*
     * 关联用户
     * */
    public function getMember()
    {
        return $this->hasOne(Member::className(), ['id' => 'member_id']);
    }
    /*
     * 关联用户
     * */
    public function getIdentity()
    {
        return $this->hasOne(Identity::className(), ['member_id' => 'member_id']);
    }




    /**
     * 写入日志
     * @param      $member_id
     * @param      $out_trade_no
     * @param      $pay_fee
     * @param null $type
     * @return bool
     */
        public static function addLogs($member_id, $out_trade_no, $pay_fee, $type = NULL)
    {
        $logModel               = new self();
        $logModel->member_id    = $member_id;
        $logModel->out_trade_no = $out_trade_no;
        $logModel->pay_fee      = $pay_fee;
        $logModel->order_group  = $type;
        $logModel->created_at   = time();
        $logModel->updated_at   = time();
        if (!$logModel->save()) {
            return FALSE;
        }

        return TRUE;
    }

    /**
     * 添加账单信息
     * @param      $member_id     用户id
     * @param      $out_trade_no  备注单号
     * @param      $pay_fee       金额
     * @param null $type
     * '资金转入', '资金转出', '订单开仓', '订单平仓',
     * '订单爆仓', '保证金', '手续费', '延期费'
     * '撤单', '用户赠金'
     * @return bool
     */
    public function addBill($member_id, $out_trade_no, $pay_fee, $type = NULL)
    {

        $logModel               = new self();
        $logModel->openid       = $member_id;
        $logModel->out_trade_no = $out_trade_no;
        $logModel->pay_fee      = $pay_fee;
        $logModel->order_group  = $type;
        $logModel->created_at   = time();
        $logModel->updated_at   = time();
        if (!$logModel->save()) {
            return FALSE;
        }

        return TRUE;

    }

}
